using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Tamir.SharpSsh;
using Tamir.Streams;
using SypConst.Garbage;


namespace SypEngine.TransferHandler
{
    public class SypScpTransferHandler : SypTransferHandler
    {
        private Scp mySctpClient = null;
        private SshStream mySshClient = null;
        private string ScpHostname = null;
        private string ScpUser = null;
        private string ScpPassword = null;
        private string RemotePath = null;
        private int Port = 22;

        public override int[] CheckSynchronizedPictures(string RootDir)
        {
            return null;
        }

        public SypScpTransferHandler(SetText outMethod, string Hostname, string User, string Password, string remotePath, int port)
            : base (outMethod)
        {
            mySctpClient = new Scp();
            ScpHostname = Hostname;
            ScpUser = User;
            ScpPassword = Password;
            RemotePath = remotePath;
            Port = port;

            try
            {
                mySshClient = new SshStream(Hostname, User, Password, port);
                mySshClient.Prompt = "#";
                mySshClient.RemoveTerminalEmulationCharacters = true;
            }
            catch (Exception e)
            {
                OutputMethod("SCP Error, open : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }

        public override void MakeDir(string DirPath)
        {
            try
            {
                mySshClient.Write("mkdir " + DirPath);
                string res = mySshClient.ReadResponse();
            }
            catch (Exception e)
            {
                OutputMethod("SCP Error, makeDir : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }

        public override void RemoveDir(string DirPath)
        {
            try
            {
                mySshClient.Write("rmdir " + DirPath);
                mySshClient.ReadResponse();
            }
            catch (Exception e)
            {
                OutputMethod("SCP Error, removeDir : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }

        public override void DeleteFile(string FilePath)
        {
            try
            {
                mySshClient.Write("rm -f " + FilePath);
                mySshClient.ReadResponse();
            }
            catch (Exception e)
            {
                OutputMethod("SCP Error, deleteFile : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }

        public override void SendFile(string LocalFilePath, string RemoteFilePath)
        {
            try
            {
                mySctpClient.To(LocalFilePath, ScpHostname, RemotePath + "/" + RemoteFilePath, ScpUser, ScpPassword, Port);
            }
            catch (Exception e)
            {
                OutputMethod("SCP Error, SendFile : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }

        public override void Close()
        {
            mySshClient.Close();
        }

        public override void SetCurrentDirectory(string Path)
        {
            try
            {
                mySshClient.Write("cd " + Path);
                mySshClient.ReadResponse();
            }
            catch (Exception e)
            {
                OutputMethod("SCP Error, SetCurrentDirectory : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }
    }
}
